import React from "react";
import { Button, Switch, message } from "antd";

/*****
 *
 * updateStatusFunc 修改状态的函数
 * record 当前表格数据行
 * actionRef 表格ref
 * recordField record里面需要取id的上级字段名
 *
 * eg: useUpdateStatus(updateStatus, record, actionRef, "admin")
 */
export default (updateStatusFunc, record, actionRef, recordField, disabled) => {
  // 修改状态
  const handleUpdateStatus = async (status, row, field) => {
    const postData = {
      id: recordField ? row[recordField].id : row.id,
      [field]: status,
    };
    const res = await updateStatusFunc(postData);
    if (res.code === 200) {
      message.success(status === 1 ? "启用成功" : "禁用成功");
      actionRef?.current.reload();
    }
  };

  const status = recordField ? record[recordField].status : record.status;

  return (
    <>
      {status === 1 ? (
        <Button
          key="disable"
          type="text"
          danger
          onClick={() => {
            handleUpdateStatus(9, record, "status");
          }}
        >
          禁用
        </Button>
      ) : (
        <Button
          key="enable"
          type="text"
          onClick={() => {
            handleUpdateStatus(1, record, "status");
          }}
        >
          启用
        </Button>
      )}
    </>
  );

  // (
  //   <Switch
  //     disabled={disabled}
  //     onChange={(e) => {
  //       handleUpdateStatus(e, record, "status");
  //     }}
  //     checked={status === 1}
  //     checkedChildren="启用"
  //     unCheckedChildren="禁用"
  //   />
  // );
};
